<?php

class VideoLogic {

    private $video_query;

    //private $cat_query;

    public function __construct() {
        $this->video_query = DB::table('video');
        //$this->cat_query = DB::table('category');
    }

    public function getVideoCatlist($video_id) {
        $catID = $this->video_query->where('id', '=', $video_id)->only('category_id');
        $ret = DB::query('SELECT parent.id, parent.name FROM category AS node,category AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.id = ? ORDER BY parent.lft', array($catID));
        if (!is_array($ret)) {
            return false;
        }
        $tmp = array_slice($ret, 1);
        return json_decode(json_encode($tmp), true);
    }

    public function getTitleByID($id) {
        $ret = DB::table('video')->find($id);
        if (empty($ret)) {
            return false;
        }
        return $ret->title;
    }

    public function getVideoInfoByID($id) {
        $id = mysql_real_escape_string($id);
        $ret = DB::query('SELECT video.id, video.title,video.duration, video.image,video.price,video.type,video.viewed,video.status,video.ctime,video.sort_order,
            video.category_id,video.file_uuid,
           (SELECT name FROM category WHERE id = video.category_id) AS category_name,
           video_description.description,video_description.tag 
           FROM video LEFT JOIN video_description
           ON video.id = video_description.id
           WHERE video.id =?  AND video.status=1
           ORDER BY sort_order DESC', array($id));
        if (empty($ret)) {
            return false;
        }
        return $ret[0];
    }

    public function getSearchData($title, $catID = null) {
        $sql = '';
        $title = mysql_real_escape_string($title);
        if (null == $catID) {
            $sql = 'SELECT video.id, video.title,video.duration, video.image,video.price,video.type,
            video.viewed,video.status,video.ctime,video.sort_order, video.category_id,
           (SELECT name FROM category WHERE id = video.category_id) AS category_name,
           left(video_description.description,30) as desc_abstract 
           FROM video LEFT JOIN video_description
           ON video.id = video_description.id
           WHERE video.title LIKE ' . "\"%$title%\"" . ' ORDER BY sort_order DESC';
        } else {
            $catID = mysql_real_escape_string($catID);
            $sql = 'SELECT video.id,  video.title,video.duration, video.image,video.price,video.type,
            video.viewed,video.status,video.ctime,video.sort_order, video.category_id,
           (SELECT name FROM category WHERE id = video.category_id) AS category_name,
           left(video_description.description,30) as desc_abstract 
           FROM video LEFT JOIN video_description
           ON video.id = video_description.id
           WHERE video.category_id =' . $catID . ' AND video.title LIKE ' . "\"%$title%\"" . ' ORDER BY sort_order DESC';
        }
        $ret = DB::query($sql);
        if (empty($ret)) {
            return array();
        }
        $catArr = array();
        foreach ($ret as $item) {
            $catArr["$item->category_id"] = array(
                $item->category_id,
                $item->category_name,
                0,
            );
        }
        foreach ($ret as $item) {
            $catArr["$item->category_id"][2]++;
        }
        $catArray = array_values($catArr);
        $videoArr = json_decode(json_encode($ret), true);

        $retArray = array(
            $catArray,
            Paginator::make($videoArr, count($videoArr), 4, false),
        );
        return $retArray;
        //echo json_encode($retArray[1]);
        //exit;
    }

    public function getVideolist($per_page) {
        $ret = DB::query('select v.id,v.title,v.duration,v.image,v.price,v.type,v.viewed,v.status,v.ctime,c.name from video as v,category as c where v.category_id = c.id;');
        $orders = Paginator::make($ret, count($ret), $per_page, false);
        return $orders;
    }

    public function getVideolistByCatID($category_id, $per_page) {

        $catArr = DB::query('SELECT node.id, node.name FROM category AS node, category AS parent, category AS sub_parent
                WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt 
                AND sub_parent.id=' . $category_id . ' GROUP BY node.name ORDER BY node.lft');
        if (empty($catArr)) {
            return false;
        }
        $idArr = array();
        foreach ($catArr as $item) {
            $idArr[] = $item->id;
        }

        $ret = DB::table('video')->where_in('category_id', $idArr)->paginate($per_page, array('id', 'category_id', 'title', 'image', 'sort_order', 'ctime'));
        if (empty($ret)) {
            return false;
        }
        return $ret;
    }

    public function getFixedVideoListByCatID($category_id) {
        $ret = DB::table('video')->where('category_id', '=', $category_id)->take(5)->get(array('id', 'title', 'image', 'sort_order', 'ctime'));
        var_dump($category_id, $ret);
        exit;
        if (empty($ret)) {
            return false;
        }
        return $ret;
    }

    public function getFixedVideoListByCatIDArr($catArr) {
        $ret = DB::table('video')->where_in('category_id', $catArr)->take(4)->get(array('id', 'category_id', 'title', 'image', 'sort_order', 'ctime'));
        //var_dump($category_id,$ret);exit;
        if (empty($ret)) {
            return array();
        }
        return $ret;
    }

    public function addVideoInfo($title, $description, $category_id, $duration, $image, $price, $type, $sort_order, $file_uuid, $status = 1, $tag = null) {
        $id = $this->video_query->insert_get_id(array(
            'title' => $title,
            'category_id' => $category_id,
            'duration' => $duration,
            'image' => $image,
            'price' => $price * 10,
            'type' => $type,
            'file_uuid' => $file_uuid,
            'sort_order' => $sort_order,
            'status' => $status,
                )
        );
        if (is_int($id)) {
            $video_desc_ret = DB::table('video_description')->insert(
                    array(
                        'id' => $id,
                        'title' => $title,
                        'description' => $description,
                        'tag' => $tag,
                    )
            );
            return $video_desc_ret;
        } else {
            return false;
        }
    }

    public function updateVideoDesc($video_id, $desc, $tag) {
        $ret = DB::table('video_description')->where('id', '=', $video_id)
                ->update(array(
            'description' => $desc,
            'tag' => $tag,
            'mtime' => time(),
        ));
        return 0 === $ret ? false : true;
    }

    public function updateVideCommInfo($video_id, $title, $category_id, $duration, $image, $price, $type, $sort_order, $status) {
        $ret = DB::table('video')->where('id', '=', $video_id)
                ->update(
                array(
                    'title' => $title,
                    'category_id' => $category_id,
                    'duration' => $duration,
                    'image' => $image,
                    'price' => $price * 10,
                    'type' => $type,
                    'sort_order' => $sort_order,
                    'status' => $status,
                )
        );

        return 0 === $ret ? false : true;
    }

    public function deleteVideo($id) {
        return DB::table('video')->where('id', '=', $id)->delete();
    }

}

?>
